package com.springBoot.springBootSysCore.modules.services.system.impl;

import com.springBoot.springBootSysCore.common.service.IBaseServiceImpl;
import com.springBoot.springBootSysCore.common.utils.returnUtil.CodeEnum;
import com.springBoot.springBootSysCore.common.utils.returnUtil.ResultJsonFormat;
import com.springBoot.springBootSysCore.modules.entity.system.SysOrganization;
import com.springBoot.springBootSysCore.modules.entity.utils.UserUtils;
import com.springBoot.springBootSysCore.modules.repository.system.SysOrganizationRepository;
import com.springBoot.springBootSysCore.modules.repository.system.SysUserRepository;
import com.springBoot.springBootSysCore.modules.services.system.SysOrganizationService;
import com.springBoot.springBootSysCore.modules.vo.TreeNodeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 机构Service实现
 * @author PSC
 * 2015年11月30日
 */
@Service
public class SysOrganizationServiceImpl extends IBaseServiceImpl<SysOrganization, String> implements SysOrganizationService {

	@Autowired
	private SysOrganizationRepository sysOrganizationRepository;
	@Autowired
	private SysUserRepository sysUserRepository;


	@Override
	public Map<String, Object> saveEntity(SysOrganization sysOrganization) {
		ResultJsonFormat rj = new ResultJsonFormat("操作成功", CodeEnum.SUCCESS.value());
		try {
			List<String> ids = sysOrganizationRepository.queryIdByName(sysOrganization.getName());
			Map<String, Object>  map = crosschecking(ids, sysOrganization.getId(), rj,"机构名不允许重复");
			if(map!=null){
				return map;
			}
			if(StringUtils.isNotBlank(sysOrganization.getId())){
				SysOrganization  dbOrganization =	sysOrganizationRepository.findOne(sysOrganization.getId());
				dbOrganization.EntityCopy(dbOrganization,sysOrganization);
			}else{
				sysOrganization.preInsert();
				sysOrganizationRepository.save(sysOrganization);
			}
		} catch (Exception e) {
			rj.setMsg("系统错误");
			rj.setFlag(CodeEnum.ERROR.value());
			e.printStackTrace();
		}
		return rj.convertResultJson();
	}
	@Override
	@Transactional
	public Map<String, Object> deleteEntity(List<String> asList) {
		ResultJsonFormat ret = new ResultJsonFormat("删除成功", CodeEnum.SUCCESS.value());
		try {
			List<SysOrganization> list = sysOrganizationRepository.findByParent(asList);
			if(list.size()>0){
				ret.setMsg("选中的机构有子级机构，不能做删除操作！请先取消关联。");
				ret.setFlag(CodeEnum.ERROR.value());
				return ret.convertResultJson();
			}
			sysOrganizationRepository.deleteOrganizationsByIds(asList);
		} catch (Exception e) {
			e.printStackTrace();
			ret.setMsg("删除失败");
			ret.setFlag(CodeEnum.ERROR.value());
		}
		return ret.convertResultJson();
	}
	@Override
	public List<TreeNodeVo> findTreeRes(String organizationId) {
		List<SysOrganization> allOrganization =  sysOrganizationRepository.findAllParent();
		List<TreeNodeVo> treeNodeLi = this.findTreeSonRes(allOrganization,organizationId);
		return treeNodeLi;
	}
	/**
	 * 给定机构集合将其转换成zTree数据(新增机构选择父级)
	 * @author PSC
	 * @param sonOrganization
	 * @param organizationId 修改时,当前的机构Id
	 * @return
	 */
	private List<TreeNodeVo> findTreeSonRes(List<SysOrganization> sonOrganization,String organizationId){
		List<TreeNodeVo> treeNodeLi = new ArrayList<TreeNodeVo>();
		for(SysOrganization organization : sonOrganization){
			if(organization.getId().equals(organizationId)){
				continue;
			}
			TreeNodeVo vo = new TreeNodeVo();
			vo.setId(organization.getId());
			vo.setName(organization.getName());
			vo.setText(organization.getName());
			if(organization.getChildrenOrganizations()!=null){
				vo.setChildren(this.findTreeSonRes(organization.getChildrenOrganizations(),organizationId));
			}
			treeNodeLi.add(vo);
		}
		return treeNodeLi;
	}
	@Override
	public List<TreeNodeVo> findOrganizationTreeOfUserUpdate(String userId) {
		List<SysOrganization>  parentOrg  = sysOrganizationRepository.findAllParent();
		List<String> userOrg = sysOrganizationRepository.findIdByUser(userId);
		return getChildrenOrganizationTree(parentOrg,userOrg);
	}
	/**
	 * 递归查询子部门
	 * @param userOrg  用户拥有部门
	 * @return
	 */
	public List<TreeNodeVo> getChildrenOrganizationTree(List<SysOrganization> parentOrg,List<String> userOrg) {
		List<TreeNodeVo> treeNodeLi = new ArrayList<TreeNodeVo>();
		for(SysOrganization childrenOrg : parentOrg){
			TreeNodeVo vo = new TreeNodeVo();
			vo.setId(childrenOrg.getId());
			vo.setName(childrenOrg.getName());
			vo.setText(childrenOrg.getName());
			if(userOrg.contains(childrenOrg.getId())){
				vo.setChecked(true);
			}
			if(childrenOrg.getChildrenOrganizations()!=null && !childrenOrg.getChildrenOrganizations().isEmpty()){
				/*	vo.setNocheck(true);*/
				/*	vo.setChkDisabled("true");*/
				vo.setChildren(this.getChildrenOrganizationTree(childrenOrg.getChildrenOrganizations(),userOrg));
			}
			treeNodeLi.add(vo);
		}	
		return treeNodeLi;
	}
	@Override
	public List<String> findIdsByLoginUser() {
		return sysOrganizationRepository.findIdByUser(UserUtils.getUser().getId());
	}
}
